package com.sctu.EMSApp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.ViewPagerAdapter;
import com.sctu.bean.ScenictourSurroundViewPagerBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 鑫爷您慢点 on 2015/12/10.
 */
public class ScenictourSurroundClickActivity extends Activity implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private TextView content, title;
    private ImageView returnImage;
    private List<View> viewList = new ArrayList<View>();
    private List<ImageView> imageViewList = new ArrayList<ImageView>();
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scenictour_srurround_after_click);
        initView();
    }

    private void initView() {
        //网络请求
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        GetVolleyHttp.getInstance(ScenictourSurroundClickActivity.this).getStringGETJsonData(UrlConfig.URL_SCENIC_SURROUND_ONE + id,
                listener, errorlistener,Long.decode("0"));
        viewPager = (ViewPager) findViewById(R.id.scenic_tour_round_after_click_viewPager);
        content = (TextView) findViewById(R.id.scenic_tour_round_after_click_text);
        title = (TextView) findViewById(R.id.scenic_tour_round_after_click_title);
        returnImage = (ImageView) findViewById(R.id.scenic_tour_round_after_click_return_button);
        viewPager.setOnPageChangeListener(ScenictourSurroundClickActivity.this);
        //设置返回
        returnImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            Log.e("Json", json);
            // 开始解析JSON
            // 反序列化，直接将JSON反序列化为Test这个Class对象
            ScenictourSurroundViewPagerBean.Results results = JSON.parseObject(json,
                    ScenictourSurroundViewPagerBean.class).getResults().get(0);
            String titleStr = results.getTitle();
            String contentStr = titleStr + ", " + results.getContent();
            //设置字体颜色
            SpannableStringBuilder builder = new SpannableStringBuilder(contentStr);
            ForegroundColorSpan span = new ForegroundColorSpan(ScenictourSurroundClickActivity.this.getResources().getColor(R.color.gold));
            builder.setSpan(span, 0, results.getTitle().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.setSpan(new AbsoluteSizeSpan(80), 0, results.getTitle().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            title.setText(titleStr);
            content.setText(builder);
            //获取图片父控件
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.scenic_tour_round_after_click_lay);
            for (int i = 0; i < results.getFileList().size(); i++) {

                LinearLayout linearLayout1 = new LinearLayout(ScenictourSurroundClickActivity.this);
                ViewGroup.LayoutParams ltp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                final ImageView itemImageView = new ImageView(ScenictourSurroundClickActivity.this);
                itemImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                GetVolleyHttp.getInstance(ScenictourSurroundClickActivity.this).setImageLoader(itemImageView,
                        R.drawable.scenerry, R.drawable.error_image, UrlConfig.URL + results.getFileList().get(i).getPath());
                linearLayout1.addView(itemImageView, ltp);
                viewList.add(linearLayout1);
                //设置小圆点
                ImageView imageView = new ImageView(ScenictourSurroundClickActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                margin.setMargins(10,0,0,0);
                imageViewList.add(imageView);
                if (results.getFileList().size() > 1) {
                    if (i == 0) {
                        // 默认选中第一张图片
                        imageView.setImageResource(R.drawable.scenic_tour_round_after_click_click);
                    } else {
                        //其他图片都设置未选中状态
                        imageView.setImageResource(R.drawable.scenic_tour_round_after_click_unclick);
                    }
                }
                //添加图片到控件
                linearLayout.addView(imageView,margin);
            }
            viewPager.setAdapter(new ViewPagerAdapter(viewList));
        }
    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(ScenictourSurroundClickActivity.this, "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();
        }
    };

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int arg0) {
        //遍历数组让当前选中图片下的小圆点设置颜色
        for (int i = 0; i < imageViewList.size(); i++) {
            imageViewList.get(i).setImageResource(R.drawable.scenic_tour_round_after_click_click);
            if (arg0 != i) {
                imageViewList.get(i)
                        .setImageResource(R.drawable.scenic_tour_round_after_click_unclick);
            }

        }


    }

    @Override
    public void onPageScrollStateChanged(int i) {
    }


    /**
     * 滑动改变标志
     *
     * @param imageView
     */
    private void setAfterClick(ImageView imageView) {

    }
}
